/**
RoomBot, a IRC bot.
Copyright (C) 2009 Joshua Lückers

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.googlecode.roombot.irc;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * This creates a new connection to a IRC Server.
 * @author Joshua Lückers
 * @version 10-11-2009
 */
public class IRCConnection implements Runnable {

    private String server;
    private int port;
    private Socket connection;
    private PrintWriter out;
    private BufferedReader in;
    private String nickname, altNickname, passwd;

    /**
     * Initialize the IRCConnection.
     * @param server The IRC server.
     * @param port The port being used by the IRC server.
     */
    public IRCConnection(String server, int port, String nickname, String altNickname, String passwd) {
        this.server = server;
        this.port = port;
        this.nickname = nickname;
        this.altNickname = altNickname;
        this.passwd = passwd;
    }

    /**
     * Create a connection with the IRC server.
     */
    @Override
    public void run() {
        try {
            //Create a socket to the server.
            System.out.println("Connecting to " +server+  " on port " +port);
            connection = new Socket(server, port);
            connection.setKeepAlive(true);
            if (connection.isConnected()) {
                System.out.println("Now connected to " +server+ " on port " +port);
            }
            else {
                System.out.println("Connection failed to " +server+ " on port " +port);
            }

            out = new PrintWriter(connection.getOutputStream(),true);
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            register();

            //Read the incomming message.
            String read;
            while ((read = in.readLine()) != null) {
                MessageParser.getInstance().parse(this,read);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Send a message to the server.
     * @param line The message to be send.
     */
    public void send(String line) {
        out.write(line+ "\r\n");
        out.flush();
        System.out.println("RAW SEND: " +line);
    }

    public void register() {
        send("NICK " +getNickname());
        send("USER " +getNickname()+ " 8 * :" +getNickname());
    }

    public void useAlternativeNick() {
        send("NICK " +getAltNickname());
    }

    /**
     * Set the nickname.
     * @param nickname The nickname to use.
     */
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    /**
     * Get the nickname being used.
     * @return String The nickname
     */
    public String getNickname() {
        return nickname;
    }

    /**
     * Set the alternative nickname.
     * This nickname is being used when the main nickname has been taken.
     * @param altNickname Alternative nickname
     */
    public void setAltNickname(String altNickname)  {
        this.altNickname = altNickname;
    }

    /**
     * Get the alternative nickname.
     * @return String The alternative nickname
     */
    public String getAltNickname() {
        return altNickname;
    }

    /**
     * Set the password to be used to authenticate.
     * @param passwd The password.
     */
    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }

    /**
     * Get the password being used for authentication.
     * @return String the password.
     */
    public String getPasswd() {
        return passwd;
    }
}